/*
@Time : 2021/2/23 16:26
@Author : 吴周海
@Mail: wzh200x@126.com
@File : PayOrderFunction.go
@Software: GoLand
*/
package function

import (
	"GameApiServer/game"
	"GameApiServer/models"
	"GameApiServer/utils"
	"github.com/beego/beego/v2/core/logs"
)

const APIKEY = "uutown123456" //API秘钥,用在某些需要加密的地方,如充值

// 从数据库初始化所有需要补单的数据
func InitNoSuccessPayOrders() {
	orders := models.FindNoSuceessOrder()
	SetNoSuccessPayOrders(orders)
}

// 充值失败
func ServerPayFailed(dp *models.PayRecordInfo) {
	// 转入到失败的队列中去
	AddNoSuccessPayOrder(dp)
}

func NewGamePayRequestMsgData(uid string, goodsId string, orderId string, price string) *[]interface{} {
	secKeyStr := uid + goodsId + orderId + price + APIKEY
	// 进行MD5加密
	secKey := utils.Md5Encode(secKeyStr)
	dataMsg := []interface{}{uid, goodsId, orderId, price, secKey}
	return &dataMsg
}

// 通知服务器充值
func NoticeServerPay(pData *models.PayRecordInfo) bool {
	api := "chongzhi_pay"
	dataMsg := NewGamePayRequestMsgData(pData.Uid, pData.GoodsId, pData.Order, pData.PayMoney)
	mKey := "K"
	sid := pData.Sid
	fMsg := game.FormatMessage(api, *dataMsg, mKey, sid)
	if fMsg != nil {
		ok, err := game.RequestGameServer(fMsg)
		if ok {
			return true
		} else {
			logs.Error("NoticeServerPay err order", pData.Order, err)
		}
	}
	return false
}
